package com.example.controller;
import com.example.entity.Users;
import com.example.service.UsersService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class UsersCtrl {
@Resource
    UsersService usersService;

@RequestMapping(value = "/users",method = {RequestMethod.GET,RequestMethod.POST})
public String findAllGoods(Model model, HttpServletRequest request,
  @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum
,@RequestParam(value = "pageSize",required = false,defaultValue = "4")Integer pageSize){
    PageInfo<Users> userslist = usersService.queryAll(pageNum,pageSize);
    model.addAttribute("userslist",userslist);
    return "userslist";
}

    @GetMapping("/users/{id}")
    public String getUserById(@PathVariable("id") int id, Model model) {
    Users users =usersService.findById(id);
    model.addAttribute("users",users);
    return "usersEdit";
    }

    @PostMapping("/editUsers")
    public String addUsers(Users users,Model model){
        System.out.println(users);
        int num = usersService.update(users);
        model.addAttribute("msg","商品信息更新成功！");
        return "usersEdit";
}

    @GetMapping("/delUsers/{id}")
    public String delGoods(@PathVariable("id") int id, Model model){
        int num =usersService.deleteByID(id);
        model.addAttribute("msg","删除成功");
        return "forward:/admin/users";
    }

    @GetMapping("/usersadd")
    public  String insertGoods(Users users, Model model){
        return "usersadd";
    }

    @PostMapping("/user2")
    public  String addGood(Users users,Model model){
        System.out.println(users);
        int num2 = usersService.insert(users);
        return "redirect:/admin/users";
    }

}
